﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iOak.Domain.Directory;
using iOak.Business.Samples;
using iOak.Domain.Users;
using System.Web.Security;
using iOak.Common;
using iOak.Business.Users;
using iOak.Business.Campaigns;
using iOak.Domain.Campaigns;
using iOak.Domain.Resumes;
using iOak.Business.Resumes;
using iOak.Common.Untity;
using iOak.Web.TemplateHelper;
using System.Xml;
using System.Configuration;

namespace iOak.Web {
    public partial class Default : BasePage {
        private WorkAssistant _workassistant = new WorkAssistant();
        private CampaignService _campaignService = new CampaignService();
        private ApplicationCandidateService _applicationCandidateService = new ApplicationCandidateService();
        private ResumeDetailService _resumeDetailService = new ResumeDetailService();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                BindCampaignList();
                BindNoticeList();
                BindNoteList();
            }
        }

        private void BindCampaignList() {
            var dataSource = _campaignService.GetListRemindCompany(_workassistant.CurrentUser.CompanyId);
            rptCampaignList.DataSource = dataSource;
            rptCampaignList.DataBind();
            plNoData.Visible = dataSource.Count == 0;
        }

        private void BindNoticeList() {
            string url = Convert.ToString(ConfigurationManager.AppSettings["noticeurl"]);

            if (string.IsNullOrEmpty(url)) {
                url = "http://www.wndhr.com/go/rss.aspx?channelurl=/zfyc/ ";
            }

            //var list = GetRss(url, 8);
            var list = GetRss(url, 5);

            rptNoticeList.DataSource = list;
            rptNoticeList.DataBind();

            plNoNotice.Visible = list.Count == 0;
        }

        private void BindNoteList() 
        {
            var dataSource = _applicationCandidateService.GetUnPassedList(_workassistant.CurrentUser.CompanyId);
            foreach (var item in dataSource) 
            {
                var resumeDetail = _resumeDetailService.GetByResumeIdAndKey(item.ResumeId, "UserName");
                item.UserName = resumeDetail.ValueString;
            }
            rptNoteList.DataSource = dataSource;
            rptNoteList.DataBind();
            phNoNote.Visible = dataSource.Count == 0;
        }

        protected void Button1_Click(object sender, EventArgs e) {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectToLoginPage();
        }

        protected void rptCampaignList_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
                Campaign campaign = e.Item.DataItem as Campaign;
                Label lblCampaign = e.Item.FindControl("lblCampaign") as Label;
                HyperLink hlInput = e.Item.FindControl("hlInput") as HyperLink;
                if (campaign.Status == (int)CampaignStatus.Appling) {
                    lblCampaign.Text = String.Format("{0}已发布，需要您填报。", campaign.Subject);
                    hlInput.Text = "填报";
                }
                //else if (campaign.Status == (int)CampaignStatus.Checking) {
                //    lblCampaign.Text = String.Format("{0}正在审核。", campaign.Subject);
                //    hlInput.Text = "浏览";
                //}
                else if (campaign.Status == (int)CampaignStatus.Closed) {
                    lblCampaign.Text = String.Format("{0}已审核结束。", campaign.Subject);
                    hlInput.Text = "浏览";
                }
                hlInput.NavigateUrl = "CandidateList.aspx?campaignId=" + campaign.CampaignId.ToString();
            }
        }

        protected List<NewsItem> GetRss(string url, int count) {
            RssChannel rssReader = new RssChannel(url);

            List<NewsItem> newslist = new List<NewsItem>();

            for (int i = 0; i < rssReader.count(); i++) {
                if (newslist.Count >= count) break;

                NewsItem news = new NewsItem();

                news.Title = Convert.ToString(rssReader[i].value["title"]);
                news.Link = Convert.ToString(rssReader[i].value["link"]);
                news.Posted = Convert.ToString(rssReader[i].value["pubDate"]);

                if (news.Posted.Length > 5) {
                    news.Posted = news.Posted.Substring(5);
                }

                if (!string.IsNullOrEmpty(news.Link) && !string.IsNullOrEmpty(news.Title))
                    newslist.Add(news);
            }

            return newslist;
        }
    }

    public class NewsItem {
        private string _title;
        private string _link;
        private string _posted;

        public string Title {
            get { return _title; }
            set { _title = value; }
        }
        public string Link {
            get { return _link; }
            set { _link = value; }
        }

        public string Posted {
            get { return _posted; }
            set { _posted = value; }
        }
    }
}